package com.itheima.reggie.controller;

import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.R;
import com.itheima.reggie.entity.dto.OrdersDto;
import com.itheima.reggie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired(required = false)
    private OrderService orderService;

    @Autowired(required = false)
    private HttpSession session;

    /**
     * 下单功能
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        Long userId = (Long) session.getAttribute("user");
        orderService.submit(orders,userId);
        return R.success("下单成功");
    }

    /**
     * 最新订单查询与历史订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>>userPage(@RequestParam(defaultValue = "1") Integer page ,
                                      @RequestParam(defaultValue = "1") Integer pageSize){
        R<Page<OrdersDto>> result = orderService.userPage(page,pageSize);
        return result;
    }


    /**
     * 订单明细功能
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page<Orders>> page(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pageSize,
                                Long number,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date beginTime,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date endTime){
        R<Page<Orders>> result = orderService.page(page,pageSize,number,beginTime,endTime);
        return result;
    }
}
